<template>
	<view class="send-address-container">
		<view class="item first">
			<text class="title">发货人</text> <input class="ipt" type="text" v-model="address.deliver" placeholder="请输入" />
		</view>
		<view class="item">
			<text class="title">手机号码</text><input class="ipt" type="text" v-model="address.phone" placeholder="请输入" />
		</view>
		<view class="item" @click="selectArea">
			<text class="title">所在地区</text> <text class="select" v-if="address.province==''">请选择</text>
			<!-- <input type="text" class="ipt area" v-model="address.province+address.city+address.area" /> -->
			<view class="pca">{{address.province+address.city+address.area}}</view>

			<image src="../../static/icons/my_data_rightarrow@3x.png" class="right-arrow"></image>
		</view>
		<view class="item">
			<text class="title">详情地址</text><input class="ipt" type="text" v-model="address.address" placeholder="请输入" />
		</view>
		<pcaPicker ref="pcaPicker" @confirm="getPCA($event)"></pcaPicker>
		<button type="warn" class="save" @click="updateSendAddress">保存</button>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import pcaPicker from '../zz-pcaPicker_1.0.0/components/pcaPicker/pcaPicker.vue'
	export default {
		components: {
			pcaPicker
		},
		data() {
			return {
				address: {
					province: '',
					city: '',
					area: '',
					nickname: '',
					phone: '',
					address: ''
				},
				addressId: '',
				prCiAr: ''
			};
		},
		onLoad() {
			this.getUserDeliverAddress()
		},
		methods: {
			// 修改发货地址
			async updateSendAddress() {
				let reg_tel =
					/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
				if (!reg_tel.test(this.address.phone)) {
					uni.$showMsg('请检查手机号!')
					return
				} else if (this.address.province == '' || this.address.contact == '' || this.address.address == '' ||
					this.address.deliver == '') {
					uni.$showMsg('请将信息填写完整!')
					return
				} else {
					const {
						data: res
					} = await uni.$http.post('/user/updateUserDeliverAddress', {
						userId: this.userInfo._id,
						province: this.address.province,
						city: this.address.city,
						area: this.address.area,
						address: this.address.address,
						deliver: this.address.deliver,
						phone: this.address.phone,
						// addressId:this.addressId
					})
					console.log(res)
					if (res.msg == '成功') {
						uni.$showMsg('地址更新成功!')
					}

				}
				// this.getUserDeliverAddress()
			},
			// 获取发货地址
			async getUserDeliverAddress() {
				const {
					data: res
				} = await uni.$http.post('/user/getUserDeliverAddress', {
					userId: this.userInfo._id
				})
				console.log(res.data)
				this.address.province = res.data.province
				this.address.city = res.data.city
				this.address.area = res.data.area
				this.address.address = res.data.address
				this.address.deliver = res.data.deliver
				this.address.phone = res.data.phone
				// this.addressId=res.data._id
			},
			// 选择区域
			selectArea() {
				this.$refs.pcaPicker.show();
			},
			getPCA(e) {
				console.log(e.province + e.city + e.area);
				this.address.province = e.province
				this.address.city = e.city
				this.address.area = e.area
				this.prCiAr = (e.province + e.city + e.area)
			},
		},
		computed: {
			...mapState('m_user', ['userInfo'])
		},
	}
</script>

<style lang="scss">
	.pca {
		text-align: right;
		width: 100%;
		padding-right: 20rpx;
	}
	.send-address-container {

		.save {
			width: 702rpx;
			height: 98rpx;
			background: #D80E0E;
			border-radius: 10rpx;
			position: fixed;
			bottom: 30rpx;
			left: 50%;
			transform: translateX(-50%);
		}

		.item {
			height: 88rpx;
			line-height: 88rpx;
			border-bottom: 1rpx solid #e6e6e6;
			padding: 0 23rpx;
			font-size: 30rpx;
			color: #111111;
			display: flex;
			position: relative;

			.select {
				font-size: 30rpx;
				font-weight: 400;
				color: #888b8d;
				position: absolute;
				right: 50rpx;
			}

			.right-arrow {
				width: 15rpx;
				height: 28rpx;
				position: absolute;
				right: 25rpx;
				top: 50%;
				transform: translateY(-50%);
			}

			&.first {
				border-top: 1rpx solid #e6e6e6;
			}

			.title {
				width: 150rpx;
			}

			.ipt {
				height: 100%;
				flex: 1;
				text-align: right;
				overflow: hidden;
				text-overflow: ellipsis;
				font-size: 30rpx;
				font-weight: 400;
				color: #111111;

				&.area {
					padding-right: 25rpx;
				}
			}
		}
	}
</style>
